package com.vajava.str.model;

import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import java.util.HashSet;
import java.util.Set;

@Entity(name = "TestFile")
public class TestFile {
    private Long id;
    private TestType type;
    private String filePath;
    private Set<TestTag> tags;

    public TestFile(TestType type, String filePath) {
        this.type = type;
        this.filePath = filePath;
        this.tags = new HashSet<TestTag>();
    }

    protected TestFile() {
    }

    public TestFile(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    @ManyToOne(optional = false, targetEntity = TestType.class)
    public TestType getType() {
        return type;
    }

    public void setType(TestType type) {
        this.type = type;
    }

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    public Set<TestTag> getTags() {
        return tags;
    }

    public void setTags(Set<TestTag> tags) {
        this.tags = tags;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        return filePath.equals(((TestFile)o).filePath);
    }

    public int hashCode() {
        return filePath.hashCode();
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
